//ajax
$("#tbl_user").bootstrapTable({
    url:"tushu/list",
    contentType:'application/x-www-form-urlencoded',
    method:'POST',
    toolbar:'#toolbar',
    sidePagination:'server',
    pageNumber:1,//当前页
    pageSize:5,//每页条数
    pageList:[5, 10, 15, 20,'ALL'],
    dataType:'json',//返回数据的类型
    cache:false,
    pagination:true,//使用分页
    showRefresh:true,//右上角刷新按钮
    showColumns:true,//右上角,列,列表
    queryParams:function(params){
        params["tu_name"] = $("#username").val();
        params["lx.lx_id"] = $("#roleCondition").val();
        console.log(params)
        return params;
    },
    classes:'table table-hover table-striped',
    columns:[{
        checkbox:true
    },{
            field:"ts_id",
            title:"主键",
            align:"center"
        },{
            field:"tu_name",
            title:"图书名称",
            align:"center"
        },{
            field:"jieyue",
            title:"借阅价格",
            align:"center"
        },{
            field:"chushou",
            title:"出售价格",
            align:"center"
        },{
            field:"fajin.name",
            title:"破损量比",
            align:"center"
        },{
            field:"cishu",
            title:"借阅次数",
            align:"center"
         },{
            field:"lx.lx_name",
            title:"类型",
            align:"center"
        },{
            field:"chubanshe.cbs_name",
            title:"出版社名字",
            align:"center"
        },{
            field:"zhuangtai.zt_name",
            title:"图书状态",
            align:"center"
        },{
            field:"book_img",
            title:"图书图片",
            align:"center",
            formatter:function (value, row, index) {
                console.log("nuhbhubu"+row.book_img)
                return '<img src="'+row.book_img+'" width="80px" />';
            }
        },{
            title:"操作",
            align:"center",
            formatter:function (value, row, index) {
                var html = [
                    "<a href='javascript:void(0);' onclick='Add(\"修改\",\"tushu/edit?ts_id="+row.ts_id+"\",\""+row.zhuangtai.zt_name+"\")'><span class='glyphicon glyphicon-pencil'></span></a>&nbsp;&nbsp;&nbsp;&nbsp;",
                    "<a href='javascript:void(0);' onclick='delRow("+row.ts_id+")'><span class='glyphicon glyphicon-remove'></span></a>",
                ];
                return html.join("");
            }
        }
    ]

});



//删除一行数据
function delRow(ts_id) {
    swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        cancelButtonText: "我再想想吧!",
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "删除",
        closeOnConfirm: false
    }, function () {
        //ajax
        $.post('tushu/delete',{"ts_id":ts_id},function (data) {
                swal("删除成功！", "您已经永久删除了这条信息。", "success");
                //刷新表格
                $("#tbl_user").bootstrapTable('refresh');
        });

    });
}

function Add(title,url,zhuangtai) {
    if(zhuangtai == '已借阅'){
        swal("错误！", "您不可以修改这条信息。", "error");
    }else{
        $("#myModal").find(".modal-title").html(title);
        $("#myModal").find(".modal-body").load(url);
        $("#myModal").modal('show');
    }
}

//显示模态框并打开新增界面
function toAdd(title,url) {
    $("#myModal").find(".modal-title").html(title);
    $("#myModal").find(".modal-body").load(url);
    $("#myModal").modal('show');
}
//提交表单
function submitForm(btn) {
    var $form = $(btn).parents("#myModal").find("#myForm");
    var action = $form.attr("action");
    // var str = $form.serialize();
    $.ajax({
        url:action,
        method:"post",
        //data:str,
        //FormData(dom)
        data:new FormData($form.get(0)),
        dataType:'json',
        processData: false,
        contentType: false,
        success:function (data) {
            if(data.result){
                swal("操作成功！",  "您完成了操作");
                //隐藏模态框
                $("#myModal").modal('hide');
                //刷新表格
                $("#tbl_user").bootstrapTable('refresh');

            }
        }
    });
}



//按条件查询
function setCondition() {
    $("#tbl_user").bootstrapTable('refresh');
}

//批量删除
function batchDel() {
    //1.判断是否有行被选中
    //getSelections
    //如果没有行被选中,返回[]
    var rows = $("#tbl_user").bootstrapTable("getSelections");
    if(rows.length == 0){
        swal("提示", "请选中要删除的行", "warning");
        return;
    }
    //2.1获取要删除的行(主键)  []
    var model = {};
    for(var i = 0;i<rows.length;i++){
        model["ids["+i+"]"] = rows[i].ts_id
}
//2.2传递数据到Controller
swal({
    title: "您确定要删除这条信息吗",
    text: "删除后将无法恢复，请谨慎操作！",
    type: "warning",
    showCancelButton: true,
    cancelButtonText: "我再想想吧!",
    confirmButtonColor: "#DD6B55",
    confirmButtonText: "删除",
    closeOnConfirm: false
}, function () {
    //ajax
    $.post('tushu/batchDel',model,function (data) {
        if(data.result){
            swal("删除成功！", "您已经永久删除了这条信息。", "success");
            //刷新表格
            $("#tbl_user").bootstrapTable('refresh');
        }
    },'json');

});
}

function batchAdd(title,url) {
    $("#modalExcel").find(".modal-title").html(title);
    $("#modalExcel").find(".modal-body").load(url);
    $("#modalExcel").modal('show');
}


function batchInsert() {
    var $form = $("#modalExcel").find('#myFormXls');
    var action = $form.attr('action');
    $.ajax({
        url:action,
        method:"post",
        data:new FormData($form.get(0)),
        dataType:'json',
        processData: false,
        contentType: false,
        success:function (data) {
            if(data.result){
                swal("添加成功！", "您已经永久添加了这些信息。", "success");
                //隐藏模态框
                $("#modalExcel").modal('hide');
                //刷新表格
                $("#tbl_user").bootstrapTable('refresh');

            }
        }
    });
}

function exportExcel(url) {
    var form = document.createElement('form');
    form.setAttribute("action",url);
    form.setAttribute("method","post");
    document.getElementsByTagName('body')[0].appendChild(form);
    form.submit();
}